#ifndef _SPEECHANALYSISCUT_H_
#define _SPEECHANALYSISCUT_H_

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#include "utils.h"

using namespace std;

class SpeechProcessState{
public:
	SpeechProcessState();
	~SpeechProcessState();
	enum ERROR_TYPE{
		CORRECT_STATE = 0,
		ERROR_READ_FILE,
		ERROR_WRITEN_FILE,
		ERROR_OTHERS
	};
};

typedef struct WaveOptions
{
	string wave_kaldi_scp = "";
	int sample_rate = 8000;
	int byte_num = 2;

};

typedef struct 
{
	char riff_id[4];         // 4 chars for storing 'RIFF'.4 chars and one '\0' can be useful for using strcmp
	unsigned  int file_size;	// 4chars, size of file
	char wave_id[4];			//4chars, "WAVE"
	char fmt_id[4];			//4chars, "fmt ", the last char is blank
	long int format_length; //4 chars
	unsigned short format_tag; //2chars
	unsigned short channels;   //2chars
	unsigned  int sample_rate;  //4chars
	unsigned  int avg_bytes_sec;  //4 chars
	unsigned short block_align; //2 chars
	unsigned short bits_per_sample;    // 2 chars, 16 bits data
	char data_id[4]; //4chars
	unsigned  int data_size; //4chars,  32 bits data
}WAV_HEADER;

class WAVE
{
public:
	WAVE(const char* filename, const char* wave_id);
	~WAVE();

	inline int WaveLength(){return m_waveHeader->data_size;}
	inline int SampleRate(){return m_waveHeader->sample_rate;}
	inline int BitsPerSample(){return m_waveHeader->bits_per_sample;}
	inline  char* GetWaveId()  {return &m_waveId[0];}
	inline  char* GetWaveDir() {return &m_waveDir[0];}
	int MakeWavHeader();
	//int PrintWaveInfo();
	inline void ChangeWaveLength(unsigned new_length){m_waveHeader->data_size = new_length;}
	inline void ChangeWaveSampleRate(unsigned new_samplerate){m_waveHeader->sample_rate = new_samplerate;}
	void GetFileName(const char* complete_dir, char* single_filename);
	int DoSpeechSplit(unsigned subseg_id, float start, float end, float offset, 
		unsigned sample_rate, const char* output_dir);
	int WriteWaveHeader(const WAV_HEADER* wave_header, FILE* fp);
	
protected:
	WAV_HEADER* getWaveHeader();
	WAV_HEADER* ReadWaveHeader(const char* file_name);
	short* ReadWaveDate(const char* file_name);
	
	short* GetWaveData(){return m_waveData;}

private:
	short* m_waveData;
	WAV_HEADER* m_waveHeader;
	char m_waveId[128];
	char m_waveDir[512];

};
class SpeechAnalysis
{
public:
	SpeechAnalysis();
	~SpeechAnalysis();

	void SpeechCutTool();
protected:
	int ReadWaveHeader();

private:
	WAVE* m_wave;


};

bool IsFileAWave(const char* file_name);
bool MakeDirectory(const std::string& dir_name);
int GetFilePath(std::string &strFilePath);

SpeechProcessState::ERROR_TYPE AddHeader4Utterance(const string& utterance_file_, int sample_rate_=8000, int bytes_width=2);
int ProcessKaldiScp(Options& wave_optons_);

WAV_HEADER* MakeWaveHeader(const unsigned long file_size_, unsigned short channel_nums_,
	unsigned long sample_rate_, unsigned long avg_bytes_sec, unsigned short bits_per_sample_);


std::vector<std::string> SplitLine(const std::string& original_line, int split_num=2);

SpeechProcessState::ERROR_TYPE enumGlobalVar(const string& );

#endif
